GMMMPEC_c_ktr_FE <- function(x) { 

x<-as.matrix(x)
  
theta1 <- x[1:(K_A+1), 1]     
theta2 <- x[((K_A+1)+1):((nD+1)*(K_A+1) + (countries-1) + nD), 1]     
delta = x[((nD+1)*(K_A+1) + (countries-1) + nD +1):((nD+1)*(K_A+1) + (countries-1) + nD + numProdsTotal), 1]               
g = x[((nD+1)*(K_A+1) + (countries-1) + nD + numProdsTotal +1):nrow(x), 1]  
g <- as.matrix(g) 

  
cong <- g - t(IV)%*%(delta - x_char[,1:(K_A+1)]%*%theta1)
  
expmu = matrix(0,nrow(x_char),ncol(v))

for (t in 1:T) { 
theta2_mat = rbind(as.matrix(theta2[1:(K_A+1)]), theta2[length(theta2)-nD+1]*as.matrix(theta2[(nD*(K_A+1)+1):(length(theta2)-nD)]))
expmu[marketStarts[t,1]:marketEnds[t,1],] = exp(x_char[marketStarts[t,1]:marketEnds[t,1],]%*%diag(as.numeric(theta2_mat))%*%v[1:(K+1),,t] + x_char[marketStarts[t,1]:marketEnds[t,1],]%*% rbind(matrix(0,K_A+1,1), as.matrix(theta2[(nD*(K_A+1)+1):(length(theta2)-nD)]))%*%matrix(1,1,nn))   
for (dd in 2:nD) {
theta2_mat = rbind(as.matrix(theta2[((dd-1)*(K_A+1)+1):(dd*(K_A+1))]), theta2[length(theta2)-nD+dd]*as.matrix(theta2[(nD*(K_A+1)+1):(length(theta2)-nD)]))   
expmu[marketStarts[t,1]:marketEnds[t,1],] = expmu[marketStarts[t,1]:marketEnds[t,1],] * exp(x_char[marketStarts[t,1]:marketEnds[t,1],]%*%diag(as.numeric(theta2_mat))%*%v[((dd-1)*(K+1)+1):(dd*(K+1)),,t]) 
}
}



expmeanval = as.matrix(exp(delta))
simEstShare<-ind_shnormMPEC(expmeanval,expmu, oo, sharesum, marketForProducts)
simShare<-simEstShare[,1:(ncol(simEstShare)-1)]
EstShare<-simEstShare[,ncol(simEstShare)]
EstShare<-as.matrix(EstShare)
share<-as.matrix(share) 
  
con<-c(rbind(EstShare - share, cong)) 


return(con)
  
}  